package com.googlecode.stockhamster.aiholder;

import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

import com.googlecode.stockhamster.Constant;
import com.googlecode.stockhamster.company.ICompany;
import com.googlecode.stockhamster.expert.IExpert;
import com.googlecode.stockhamster.rumor.IRumor;
import com.googlecode.stockhamster.stockstat.IStockStat;
import com.googlecode.stockhamster.utils.XFireStub;

public class AIEnvironment {
	private ICompany company;
	private IRumor rumor;
	private IStockStat status;
	private IExpert expert;

	public String[] cList;
	public List<Integer> stockIdList = new ArrayList<Integer>();
	public List<Long> currPrize = new ArrayList<Long>();
	public List<Long> exptPrize1 = new ArrayList<Long>();
	public List<Long> exptPrize2 = new ArrayList<Long>();
	public List<Long> exptPrize3 = new ArrayList<Long>();

	public AIEnvironment() throws MalformedURLException {
		company = new XFireStub<ICompany>(Constant.companyUrl, ICompany.class)
				.getStub();
		rumor = new XFireStub<IRumor>(Constant.rumorUrl, IRumor.class)
				.getStub();
		status = new XFireStub<IStockStat>(Constant.stockStatUrl,
				IStockStat.class).getStub();
		expert = new XFireStub<IExpert>(Constant.expertUrl, IExpert.class)
				.getStub();

		cList = company.getCompanyList(Constant.companyAdminCode);
		for (String name : cList)
			stockIdList.add(company.getStockID(name));
	}

	public void RefreshAll() {
		exptPrize1.clear();
		exptPrize2.clear();
		exptPrize3.clear();
		for (int i = 0; i < cList.length; i++) {
			Integer stockID = stockIdList.get(i);
			Long capital = company.getCapital(stockID);
			Integer issueSize = company.getIssueSize(stockID);

			Double categoryRatio1 = rumor.getCategoryRatio(1, company
					.getCategory(cList[i]));
			Double companyRatio1 = rumor.getCompanyRatio(1, cList[i]);
			Double categoryRatio2 = rumor.getCategoryRatio(2, company
					.getCategory(cList[i]));
			Double companyRatio2 = rumor.getCompanyRatio(2, cList[i]);
			Double categoryRatio3 = rumor.getCategoryRatio(3, company
					.getCategory(cList[i]));
			Double companyRatio3 = rumor.getCompanyRatio(3, cList[i]);

			exptPrize1.add(expert.evaluation(capital, issueSize, companyRatio1,
					categoryRatio1));
			exptPrize2.add(expert.evaluation(capital, issueSize, companyRatio2,
					categoryRatio2));
			exptPrize3.add(expert.evaluation(capital, issueSize, companyRatio3,
					categoryRatio3));
		}
		RefreshPrice();
	}

	public void RefreshPrice() {
		currPrize.clear();
		for (Integer stockId : stockIdList)
			currPrize.add(status.getPrice(stockId));
	}
}
